package org.cos.verification.verifier;


import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class VerifierGUI extends MIDlet implements CommandListener {
	
	private Display display;

	private MainScreen mainScreen = null;
	private RuleMgrScreen ruleMgrScreen = null;
	private RuleViewerScreen ruleViewerScreen = null;
	private RuleEditorScreen ruleEditorScreen = null;
	private OpenFileScreen openFileScreen = null;
	private CheckProgressScreen checkProgressScreen = null;
	private CheckReportScreen checkReportScreen = null;
	public VerifierGUI() {
		System.out.println("init ... ");
		display = Display.getDisplay(this);
	}
	
	public void initScreen(){
		// Init all Screens
		mainScreen = (MainScreen) ScreenFactory.getInstance().Create(ScreenType.MAIN);
		ruleMgrScreen = (RuleMgrScreen) ScreenFactory.getInstance().Create(ScreenType.RULE_MGR);
		ruleViewerScreen = (RuleViewerScreen) ScreenFactory.getInstance().Create(ScreenType.RULE_VIEWER);
		ruleEditorScreen = (RuleEditorScreen) ScreenFactory.getInstance().Create(ScreenType.RULE_EDTIOR);
		openFileScreen = (OpenFileScreen) ScreenFactory.getInstance().Create(ScreenType.OF_DIALOG);
		try {
			//DEBUG
			checkProgressScreen = (CheckProgressScreen) ScreenFactory.getInstance().Create(ScreenType.CHK_PROG);
		} catch (Exception e) {
			Alert alert = new Alert("Error!");
			alert.setType(AlertType.ERROR);
			alert.setString(e.getMessage());
			Display.getDisplay(this).setCurrent(alert);
		}
		checkReportScreen = CheckReportScreen.getInstance();
		
		mainScreen.initUI(this).initCommand().setCommandListener(this);
		ruleMgrScreen.initUI(this).initCommand().setCommandListener(this);
		ruleViewerScreen.initUI(this).initCommand().setCommandListener(this);
		ruleEditorScreen.initUI(this).initCommand().setCommandListener(this);
		openFileScreen.initUI(this).initCommand().setCommandListener(this);
		checkProgressScreen.initUI(this).initCommand().setCommandListener(this);
		checkReportScreen.initUI(this).initCommand().setCommandListener(this);
		
		System.out.println("init ...OK");
	}
	
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		RuleManager.getInstance().shutdown();

	}

	protected void pauseApp() {
		;
	}

	protected void startApp() throws MIDletStateChangeException {
		System.out.println("Start App");
		initScreen();
		display.setCurrent(MainScreen.getInstance().screen);
		System.out.println("Start App OK.");

	}

	public void commandAction(Command cmd, Displayable screen) {
		if (screen == mainScreen.screen){
			mainScreen.crackCommand(this, cmd, screen);
		} else if (screen == ruleEditorScreen.screen){
			ruleEditorScreen.crackCommand(this, cmd, screen);
		} else if (screen == ruleMgrScreen.screen) {
			ruleMgrScreen.crackCommand(this, cmd, screen);
		} else if (screen == ruleViewerScreen.screen){
			ruleViewerScreen.crackCommand(this, cmd, screen);
		} else if (screen == openFileScreen.screen) {
			openFileScreen.crackCommand(this, cmd, screen);
		} else if (screen == checkProgressScreen.screen) {
			checkProgressScreen.crackCommand(this, cmd, screen);
		} else if (screen == checkReportScreen.screen){
			checkReportScreen.crackCommand(this, cmd, screen);
		}
	}


}
